{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Welcome to AdalFlow 🤗\n",
    "AdalFlow is your go-to library for building and auto-optimizing LLM task pipelines. We're dedicated to providing an exceptional LLM application development experience.\n",
    "\n",
    "For questions or support, [join our Discord community](https://discord.gg/ezzszrRZvT).\n",
    "\n",
    "⭐ [Star us on GitHub!](https://github.com/SylphAI-Inc/AdalFlow) ⭐\n",
    "\n",
    "### Quick Links\n",
    "* [GitHub Repository](https://github.com/SylphAI-Inc/AdalFlow)\n",
    "* [Full Tutorials](https://adalflow.sylph.ai/index.html#)\n",
    "* [Developer Notes](https://adalflow.sylph.ai/tutorials/index.html)\n",
    "* [Use Cases](https://adalflow.sylph.ai/use_cases/index.html)\n",
    "\n",
    "### Authors\n",
    "This notebook was created by [Nicholas Fleischauer](https://www.linkedin.com/in/unobtainiumrock/).\n",
    "\n",
    "### Outline\n",
    "\n",
    "* Install AdalFlow\n",
    "* Use Fireworks API in AdalFlow generator\n",
    "\n",
    "Start by setting up your `Fireworks AI` account and obtaining your API key at [here](https://fireworks.ai/login).  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import clear_output\n",
    "\n",
    "!pip install -U adalflow[fireworks-ai]\n",
    "\n",
    "clear_output()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 😇 Have Fun!\n",
    "\n",
    "Let's get started!  🚀\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "API keys have been set.\n"
     ]
    }
   ],
   "source": [
    "from getpass import getpass\n",
    "import os\n",
    "\n",
    "FIREWORKS_API_KEY = getpass(\"Please enter your Mistral API key:\")\n",
    "\n",
    "os.environ[\"FIREWORKS_API_KEY\"] = FIREWORKS_API_KEY\n",
    "\n",
    "print(\"API keys have been set.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Environment variable FIREWORKS_API_KEY must be set",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 5\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01madalflow\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcomponents\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmodel_client\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m FireworksClient\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01madalflow\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m \u001b[38;5;21;01madal\u001b[39;00m\n\u001b[1;32m      4\u001b[0m generator \u001b[38;5;241m=\u001b[39m adal\u001b[38;5;241m.\u001b[39mGenerator(\n\u001b[0;32m----> 5\u001b[0m     model_client\u001b[38;5;241m=\u001b[39m\u001b[43mFireworksClient\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m,\n\u001b[1;32m      6\u001b[0m     model_kwargs\u001b[38;5;241m=\u001b[39m{\n\u001b[1;32m      7\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmodel\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maccounts/fireworks/models/llama-v3p1-8b-instruct\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m      8\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtemperature\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;241m0.7\u001b[39m,\n\u001b[1;32m      9\u001b[0m     },\n\u001b[1;32m     10\u001b[0m )\n\u001b[1;32m     12\u001b[0m \u001b[38;5;66;03m# Provide a single prompt string\u001b[39;00m\n\u001b[1;32m     13\u001b[0m prompt_kwargs \u001b[38;5;241m=\u001b[39m {\n\u001b[1;32m     14\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124minput_str\u001b[39m\u001b[38;5;124m\"\u001b[39m: \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mHello from Fireworks AI! Can you summarize the concept of quantum mechanics?\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m     15\u001b[0m }\n",
      "File \u001b[0;32m~/Documents/test/LightRAG/adalflow/adalflow/utils/lazy_import.py:171\u001b[0m, in \u001b[0;36mLazyImport.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m    169\u001b[0m log\u001b[38;5;241m.\u001b[39mdebug(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCreating class instance: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mclass_\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m    170\u001b[0m \u001b[38;5;66;03m# normal class initialization\u001b[39;00m\n\u001b[0;32m--> 171\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mclass_\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/Documents/test/LightRAG/adalflow/adalflow/components/model_client/fireworks_client.py:47\u001b[0m, in \u001b[0;36mFireworksClient.__init__\u001b[0;34m(self, api_key, chat_completion_parser, input_type, base_url, env_api_key_name)\u001b[0m\n\u001b[1;32m     39\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__init__\u001b[39m(\n\u001b[1;32m     40\u001b[0m     \u001b[38;5;28mself\u001b[39m,\n\u001b[1;32m     41\u001b[0m     api_key: Optional[\u001b[38;5;28mstr\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m     45\u001b[0m     env_api_key_name: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFIREWORKS_API_KEY\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m     46\u001b[0m ):\n\u001b[0;32m---> 47\u001b[0m     \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__init__\u001b[39;49m\u001b[43m(\u001b[49m\n\u001b[1;32m     48\u001b[0m \u001b[43m        \u001b[49m\u001b[43mapi_key\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mapi_key\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     49\u001b[0m \u001b[43m        \u001b[49m\u001b[43mchat_completion_parser\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mchat_completion_parser\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     50\u001b[0m \u001b[43m        \u001b[49m\u001b[43minput_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minput_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     51\u001b[0m \u001b[43m        \u001b[49m\u001b[43mbase_url\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbase_url\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     52\u001b[0m \u001b[43m        \u001b[49m\u001b[43menv_api_key_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43menv_api_key_name\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     53\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/Documents/test/LightRAG/adalflow/adalflow/components/model_client/openai_client.py:180\u001b[0m, in \u001b[0;36mOpenAIClient.__init__\u001b[0;34m(self, api_key, chat_completion_parser, input_type, base_url, env_api_key_name)\u001b[0m\n\u001b[1;32m    178\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_env_api_key_name \u001b[38;5;241m=\u001b[39m env_api_key_name\n\u001b[1;32m    179\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_url \u001b[38;5;241m=\u001b[39m base_url\n\u001b[0;32m--> 180\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msync_client \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minit_sync_client\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    181\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39masync_client \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m  \u001b[38;5;66;03m# only initialize if the async call is called\u001b[39;00m\n\u001b[1;32m    182\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchat_completion_parser \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m    183\u001b[0m     chat_completion_parser \u001b[38;5;129;01mor\u001b[39;00m get_first_message_content\n\u001b[1;32m    184\u001b[0m )\n",
      "File \u001b[0;32m~/Documents/test/LightRAG/adalflow/adalflow/components/model_client/openai_client.py:191\u001b[0m, in \u001b[0;36mOpenAIClient.init_sync_client\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m    189\u001b[0m api_key \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_api_key \u001b[38;5;129;01mor\u001b[39;00m os\u001b[38;5;241m.\u001b[39mgetenv(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_env_api_key_name)\n\u001b[1;32m    190\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m api_key:\n\u001b[0;32m--> 191\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m    192\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mEnvironment variable \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_env_api_key_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m must be set\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    193\u001b[0m     )\n\u001b[1;32m    194\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m OpenAI(api_key\u001b[38;5;241m=\u001b[39mapi_key, base_url\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mbase_url)\n",
      "\u001b[0;31mValueError\u001b[0m: Environment variable FIREWORKS_API_KEY must be set"
     ]
    }
   ],
   "source": [
    "from adalflow.components.model_client import FireworksClient\n",
    "import adalflow as adal\n",
    "\n",
    "generator = adal.Generator(\n",
    "    model_client=FireworksClient(),\n",
    "    model_kwargs={\n",
    "        \"model\": \"accounts/fireworks/models/llama-v3p1-8b-instruct\",\n",
    "        \"temperature\": 0.7,\n",
    "    },\n",
    ")\n",
    "\n",
    "prompt_kwargs = {\n",
    "    \"input_str\": \"Hello from Fireworks AI! Can you summarize the concept of quantum mechanics?\"\n",
    "}\n",
    "\n",
    "response = generator(prompt_kwargs)\n",
    "\n",
    "if response.error:\n",
    "    print(f\"[Fireworks] Generator error: {response.error}\")\n",
    "else:\n",
    "    print(\"[Fireworks] LLM output:\")\n",
    "    print(response.data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
